package com.hamizhong.iflowers.db.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.hamizhong.iflowers.db.dao.base.MyBaseHibernateDao;
import com.hamizhong.iflowers.db.dao.hibernate.ICommentDao;
import com.hamizhong.iflowers.db.entity.Comment;
import com.hamizhong.iflowers.db.entity.Goods;

public class CommentDaoImpl extends MyBaseHibernateDao<Comment, Integer> implements ICommentDao {

	public List<Comment> findByGoodsId(final Integer goodsId, final Integer pageNum, final int pageSize) {
		List list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = null;
					String hql = "from Comment t ";
					hql += " where t.goods =:g ";
					query = session.createQuery(hql);
					query.setInteger("g", goodsId);
					query.setFirstResult((pageNum-1)*pageSize);
					query.setMaxResults(pageSize);
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

}
